-- receive an input file on the command line and write
-- 3 greyscale output files, one for each channel of the input.
-- produces 3 files named like the input file with a resp. '-r', '-g', '-b'
-- appended to the basename.

-- split the input filename into the basename and a suffix.
b, e, prefix, suffix = string.find (args[1], "^(.*)(%.%a+)$")
-- load the image
input = node 'gegl:load' { path = args[1] }

-- create 3 images using the mono-mixer node
r = node 'gegl:mono-mixer' { input = input, red = 1, green = 0, blue = 0}
g = node 'gegl:mono-mixer' { input = input, red = 0, green = 1, blue = 0}
b = node 'gegl:mono-mixer' { input = input, red = 0, green = 0, blue = 1}

-- save the separate channels
for channel,node in pairs ({r=r, g=g, b=b}) do
  filename = string.format ("%s-%s%s", prefix, channel, suffix)
  print ("writing", filename)
  node:save (filename)
end

